///////////////////////////////////////////////////////////////////
// Student name:Jon Hersack and Caleb Reinking
// Course: COSC 3403
// Assignment: #7 - Initial Software Construction
// File name:ComputerPlayer.java
// Purpose: Extends Player class and overrides executeTurn to provide
//          Computer player turn logic.
// Assumptions:none known
// Limitations:turn logic not yet implemented.
// Development Computer:Intel core2 duo 2.67Ghz
// Operating System: windows
// Integrated Development Environment (IDE): netbeans 6.5
// Compiler: Javac: java 1.6.0_13
///////////////////////////////////////////////////////////////////


public class ComputerPlayer extends Player
{

    ComputerPlayer()
    {
        super();
    }

    ComputerPlayer( Boolean humanControl, int team, String s )
    {
        super(false, team, s);
    }

    //############################################
    @Override
    char executeTurn()
    {
        Table myTable = Table.getInstanceOf(); 
        int otherTeam = (teamIBelongTo+1)%2;
        int returnIndex = 8;

        Card myBP = myTable.teamPiles[teamIBelongTo].get(0).topCard();
        Card mySP = myTable.teamPiles[teamIBelongTo].get(1).topCard();
        Card theirBP = myTable.teamPiles[otherTeam].get(0).topCard();
        Card theirSP = myTable.teamPiles[otherTeam].get(1).topCard();

        while(true)
        {
            // If there is not a roll card on our battle pile
            if( myTable.hazardStatus[teamIBelongTo][0] )//we are stopped
            {
               // System.out.println( "Roll Card not on our Battle Pile ");

                // If there is a hazard card on our battle pile
                if( myTable.hazardStatus[teamIBelongTo][1] ||
                    myTable.hazardStatus[teamIBelongTo][2] ||
                    myTable.hazardStatus[teamIBelongTo][3] ||
                    myTable.hazardStatus[teamIBelongTo][4] )
                {
                   // System.out.println( "Hazard Card on our Battle Pile ");
                   // String typeOfHazard = myBP.toStringRep().get(1);
                    //order: 0:stop, 1:speedLimit, 2:out of gas, 3:flattire, 4:accident
                    //check for coup fourre, then for remedy card

                    if( myTable.hazardStatus[teamIBelongTo][2] ){
                        if( playerHand.indexOfCardBySubType("EXTRA") != -1 )
                        {//i ahve a safety extra tank, play it
                            returnIndex = playerHand.indexOfCardBySubType("EXTRA");
                            break;
                        }
                        else if( playerHand.indexOfCardBySubType("GAS") != -1 )
                        {
                            //Play the Gas card
                            returnIndex = playerHand.indexOfCardBySubType("GAS");
                            break;
                        }
                    }
                    //accident
                    else if( myTable.hazardStatus[teamIBelongTo][4]  ){
                        if( playerHand.indexOfCardBySubType("DRIVING") != -1 )
                        {//i have a safety driving ace, play it
                            returnIndex = playerHand.indexOfCardBySubType("DRIVING");
                            break;
                        }
                        else if( playerHand.indexOfCardBySubType("REPAIR") != -1 )
                        {
                            //Play the Repair Remedy card
                            returnIndex = playerHand.indexOfCardBySubType("REPAIR");
                            break;
                        }
                    }
                    //flat tire
                    else if( myTable.hazardStatus[teamIBelongTo][3]  ){
                        if( playerHand.indexOfCardBySubType("PUNCT") != -1 )
                        {//i have a safety punct proof, play it
                            returnIndex = playerHand.indexOfCardBySubType("PUNCT");
                            break;
                        }
                        else if( playerHand.indexOfCardBySubType("SPARE") != -1 )
                        {
                            //Play the Spare Tire Remedy card
                            returnIndex = playerHand.indexOfCardBySubType("SPARE");
                            break;
                        }
                    }
                    //speed limit
                    else if ( myTable.hazardStatus[teamIBelongTo][1])
                    {
                        if( playerHand.indexOfCardBySubType("RIGHT") != -1 )
                        {//i have a safety right of way, play it
                            returnIndex = playerHand.indexOfCardBySubType("RIGHT");
                            break;
                        }
                        else if( playerHand.indexOfCardBySubType("END OF") != -1 )
                        {   //PLAY END OF LIMIT
                            returnIndex = playerHand.indexOfCardBySubType("END OF");
                            break;
                        }
                        else if ( playerHand.indexOfCardBySubType("ROLL") != -1)
                        {//if we dont have right, or end, still play roll if possible
                            returnIndex = playerHand.indexOfCardBySubType("ROLL");
                            break;
                        }//end
                    }
                }
                else//we are just stopped
                {
                    if( playerHand.indexOfCardBySubType("RIGHT") != -1 )
                    {//i have a safety right of way, play it
                        returnIndex = playerHand.indexOfCardBySubType("RIGHT");
                        break;
                    }
                    else if( playerHand.indexOfCardBySubType("ROLL") != -1 )
                    {
                        // Play the Roll Remedy card on the pile
                        returnIndex = playerHand.indexOfCardBySubType("ROLL");
                        break;
                    }
                }

            } // end if no roll card on top of pile

            // at this point, the roll card is on our battle pile

            //if we have a speedlimit play safety or remedy if we can
            if( myTable.hazardStatus[teamIBelongTo][1] ){
               if( playerHand.indexOfCardBySubType("RIGHT") != -1 )
               {
                   returnIndex = playerHand.indexOfCardBySubType("RIGHT");
                   break;
               }
               else if( playerHand.indexOfCardBySubType("END OF") != -1 )
               {
                   // Play the Roll Remedy card on the pile
                   returnIndex = playerHand.indexOfCardBySubType("END OF");
                   break;
               }
            } // end Speed Limit Hazard Counter-attack!!!! X-Wings unite!

            //Play first safety card if the player has one
            if( playerHand.indexOfCardByType("SAFETY") != -1 )
            {
               // System.out.println("Trying to play a safety card");
                returnIndex = playerHand.indexOfCardByType("SAFETY");
                break;
            }
           //-----------------------------------
            // try to play a hazard card if you dare...//
            if(      !myTable.hazardStatus[otherTeam][0] &&
                     !myTable.hazardStatus[otherTeam][1] &&
                     !myTable.hazardStatus[otherTeam][2] &&
                     !myTable.hazardStatus[otherTeam][3] &&
                     !myTable.hazardStatus[otherTeam][4] )
            {
                //System.out.println( "Trying to play a Hazard Card ");
                if( playerHand.indexOfCardBySubType("OUT OF") != -1 &&
                    !myTable.safetyArea[otherTeam][3] )
                {
                    returnIndex = playerHand.indexOfCardBySubType("OUT OF");
                    break;
                }
                else if( playerHand.indexOfCardBySubType("ACCID") != -1 &&
                         !myTable.safetyArea[otherTeam][1] )
                {
                    returnIndex = playerHand.indexOfCardBySubType("ACCID");
                    break;
                }
                else if( playerHand.indexOfCardBySubType("FLAT") != -1 &&
                         !myTable.safetyArea[otherTeam][2] )
                {
                    returnIndex = playerHand.indexOfCardBySubType("FLAT");
                    break;
                }
                else if( playerHand.indexOfCardBySubType("STOP") != -1 &&
                         !myTable.safetyArea[otherTeam][0] )
                {
                    returnIndex = playerHand.indexOfCardBySubType("STOP");
                    break;
                }
                else if( playerHand.indexOfCardBySubType("SPEED") != -1 &&
                         !myTable.safetyArea[otherTeam][0] )
                {
                    returnIndex = playerHand.indexOfCardBySubType("SPEED");
                    break;
                }
            } // now we have tried to play saftey/remedy hazards, play a roll card,
              //  and tried to hazard the other team

            if( !myTable.hazardStatus[teamIBelongTo][0] )
            {
                int mileage = ScoreKeeper.getInstanceOf().milesTraveled[teamIBelongTo];
                //System.out.println("Nothing devious to do...Try to play a distance card. ");
                int highestValue = 0;
                int tempID = 0;
                int tempDist = 0;
                for( int i = 0 ; i < playerHand.size() ; i++ )
                {//check number of twohundreds played
                    tempID = playerHand.getCard(i).getTypeID();
                   if(tempID >= 1 && tempID <= 5)
                   tempDist = Integer.parseInt(playerHand.getCard(i).toStringRep().get(1));

                   if(myTable.hazardStatus[teamIBelongTo][1])//speed limit
                    {                          
                        if( tempID >= 1 && tempID <= 2 && tempID > highestValue &&(1000-mileage - tempDist)>=0)
                            highestValue = tempID;//limit to 25/50 cards
                    }//end if speed limit
                   
                    else if(myTable.twoHundredsLeft[teamIBelongTo]>0)
                    {                           
                         if( tempID >= 1 && tempID <= 5 && tempID > highestValue &&(1000-mileage - tempDist)>=0)
                            highestValue = tempID;
                    }//end if twohundreds left

                    else
                    {                      
                         if( tempID >= 1 && tempID <= 4 && tempID > highestValue &&(1000-mileage - tempDist)>=0)
                            highestValue = tempID;     
                    }//we have no twohundreds left and no limit
                }
                if( highestValue != 0 )//we have a distance card to play
                {
                    returnIndex = playerHand.indexOfCardById(highestValue);
                }//end if distance card present
                else returnIndex = 8;

                break;
            }
            break;
        }// end infinite loop
        returnIndex++;//compensate for index shift of doplay
       // System.out.println("returnIndex = " + returnIndex );

        console.nextLine();
        return Integer.toString(returnIndex).charAt(0);

    }//end executeTurn


}//end class ComputerPlayer
